#include "oled_menu.h"

extern u8g2_t u8g2;
uint8_t need_confirm = 0;

menu_property_t main_menu_property =
{
  .menu_len = 4,
};

menu_t main_menu[5] =
{
  {&main_menu_property, "AT32F435 LORA", NULL,NULL, NULL},
  {&main_menu_property, "Buzzer Test", NULL,NULL, NULL},
  {&main_menu_property, "LORA Send", NULL,NULL, NULL},
  {&main_menu_property, "Uart Test", NULL,NULL, NULL},
};

menu_property_t buzzer_menu_property =
{
  .menu_len = 7,
};

// do?re?mi?fa?sol?la?si
menu_t buzzer_menu[7] =
{
  {&buzzer_menu_property, "DO", buzzer_do,(struct menu_t*)main_menu, NULL},
  {&buzzer_menu_property, "RE", buzzer_re,(struct menu_t*)main_menu, NULL},
  {&buzzer_menu_property, "MI", buzzer_mi,(struct menu_t*)main_menu, NULL},
  {&buzzer_menu_property, "FA", buzzer_fa,(struct menu_t*)main_menu, NULL},
  {&buzzer_menu_property, "SOL", buzzer_sol,(struct menu_t*)main_menu, NULL},
  {&buzzer_menu_property, "LA", buzzer_la,(struct menu_t*)main_menu, NULL},
  {&buzzer_menu_property, "SI", buzzer_si,(struct menu_t*)main_menu, NULL},
};

menu_property_t lora_menu_property =
{
  .menu_len = 8,
};

menu_t lora_menu[8] =
{
  {&lora_menu_property, "SEND Hello", lora_send_test, (struct menu_t*)main_menu, NULL},
  {&lora_menu_property, "SEND DO", lora_do, (struct menu_t*)main_menu, NULL},
  {&lora_menu_property, "SEND RE", lora_re, (struct menu_t*)main_menu, NULL},
  {&lora_menu_property, "SEND MI", lora_mi, (struct menu_t*)main_menu, NULL},
  {&lora_menu_property, "SEND FA", lora_fa, (struct menu_t*)main_menu, NULL},
  {&lora_menu_property, "SEND SOL", lora_sol, (struct menu_t*)main_menu, NULL},
  {&lora_menu_property, "SEND LA", lora_la, (struct menu_t*)main_menu, NULL},
  {&lora_menu_property, "SEND SI", lora_si, (struct menu_t*)main_menu, NULL},
};

menu_property_t uart_menu_property =
{
  .menu_len = 1,
};

menu_t uart_menu[8] =
{
  {&uart_menu_property, "SEND test", NULL, (struct menu_t*)main_menu, NULL},
};

menu_t *current_menu = main_menu;
uint8_t scroll_num = 0;
int8_t select_item = 0;

void say_hello(void)
{
  printf("hello\r\n");
  u8g2_ClearBuffer(&u8g2);
  u8g2_DrawStr(&u8g2, 10,10,"Hello world");
  u8g2_SendBuffer(&u8g2);
  vTaskDelay(pdMS_TO_TICKS(3000));
  menu_refresh();
}

void lora_send_test(void)
{
  char *send_msg = "Hello world";
  llcc68_send((uint8_t *)send_msg, strlen(send_msg));
}

void oled_menu_init(void)
{
  main_menu[1].children_menu = (struct menu_t*)buzzer_menu;
  main_menu[2].children_menu = (struct menu_t*)lora_menu;
	main_menu[3].children_menu = (struct menu_t*)uart_menu;

  //u8g2_ClearBuffer(&u8g2);
  u8g2_SetFont(&u8g2, u8g2_font_6x13_tf);
  u8g2_SetFontPosTop(&u8g2);
  // u8g2_DrawStr(&u8g2, 0, 26, "System Started");
  //u8g2_SendBuffer(&u8g2);
  menu_refresh();
}

void menu_refresh(void)
{
  u8g2_ClearBuffer(&u8g2);
  uint8_t point_i = select_item > 3 ? 3:select_item;
  u8g2_DrawStr(&u8g2, 0, point_i * 16, ">");

  for(uint8_t i = 0; i < 4; i++)
  {
    u8g2_DrawStr(&u8g2, 8, i * 16, current_menu[i + scroll_num].disp_str);
  }
  u8g2_SendBuffer(&u8g2);
  //debug_printf("%d\r\n", select_item);
}

void menu_up(void)
{
  uint8_t menu_len = current_menu->menu_property->menu_len;
  int8_t new_item = select_item - 1;
  if(new_item == -1)
  {
    return;
  }
  if(new_item >= 3)
  {
    scroll_num = new_item - 3;
  }
  // commit
  select_item = new_item;
  menu_refresh();
}

void menu_down(void)
{
  if(need_confirm)
  {
    need_confirm = 0;
    menu_refresh();
    return;
  }
  uint8_t menu_len = current_menu->menu_property->menu_len;
  int8_t new_item = select_item + 1;
  if(new_item == menu_len)
  {
    return;
  }
  if(new_item >= 3)
  {
    scroll_num = new_item - 3;
  }

  // commit
  select_item = new_item;
  menu_refresh();
}

void menu_go(void)
{
  if(current_menu[select_item].func1 != NULL)
  {
    current_menu[select_item].func1();
    return;
  }
  if(current_menu[select_item].children_menu != NULL)
  {
    current_menu = (menu_t*)current_menu[select_item].children_menu;
  }
  scroll_num = 0;
  select_item = 0;
  menu_refresh();
}

void menu_back(void)
{
  if(current_menu[select_item].father_menu != NULL)
  {
    current_menu = (menu_t*)current_menu[select_item].father_menu;
  }
  scroll_num = 0;
  select_item = 0;
  menu_refresh();
}

void menu_confirm(char* tip)
{
  u8g2_ClearBuffer(&u8g2);
  u8g2_DrawStr(&u8g2, 10, 30, tip);
  u8g2_DrawStr(&u8g2, 10, 46, "Right to Confirm");
  u8g2_SendBuffer(&u8g2);
  need_confirm = 1;
}